package com.learn.sb.redis.reids.demo;

import com.learn.sb.redis.config.RedisConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;
import java.util.StringJoiner;

/**
 * 单机配置 + 客户端配置 = jedis连接工厂
 * 一条连接只处理一条命令
 */
public class RedisTemplateDemo {

	public static void main(String[] args) {
		ApplicationContext ctx = new AnnotationConfigApplicationContext(RedisConfig.class);
		RedisTemplate redisTemplate = ctx.getBean(RedisTemplate.class);
		Object strxx = redisTemplate.opsForValue().get("city_1");
		System.out.println(strxx);
		redisTemplate.opsForValue().set("aaasss", "123123");
		redisTemplate.opsForHash().put("hash", "field", "hvalue");

		BoundListOperations listOps = redisTemplate.boundListOps("list2");
		String s1 = String.valueOf(listOps.rightPop());
		System.out.println("s1 ==>" + s1);
		String s2 = String.valueOf(listOps.index(1));
		System.out.println("s2 ==>" + s2);
		listOps.leftPush("v0");
		Long size = listOps.size();
		System.out.println("size ==>" + size);
		List elems = listOps.range(0, size);

		StringJoiner sj = new StringJoiner(",");
		elems.stream().forEach(elem -> sj.add(String.valueOf(elem)));
		System.out.println(sj.toString());

	}
}
